//
//  SetupViewController.m
//  APJ Resource
//
//  Created by Richard Zhao on 2/25/14.
//  Copyright (c) 2014 APJ. All rights reserved.
//

#import "SetupViewController.h"
#import "syncDataService.h"
#import "webService.h"
#import "Const.h"

@interface SetupViewController ()

@end

@implementation SetupViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"Setup" image:[UIImage imageNamed:@"icon_home"] tag:0];
        [self setTabBarItem:item];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self updateSyncLabel];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)syncData:(id)sender {
    [inProcess startAnimating];
    [webService loginAccount:@"admin" withPassword:@"admin"];
    [[syncDataService sharedService] doSyncDataFromWebService];
    [self updateSyncLabel];
    [inProcess stopAnimating];
}

- (void)updateSyncLabel
{
    NSTimeInterval syncTimestamp = [[NSUserDefaults standardUserDefaults] doubleForKey:SYNC_TIMESTAMP_KEY];
    if (syncTimestamp < 1) {
        lastSyncLabel.text = @"从未同步数据！";
        return;
    }
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString = [format stringFromDate:[NSDate dateWithTimeIntervalSince1970:syncTimestamp]];
    lastSyncLabel.text = dateString;
}
@end
